﻿// MIT License
// 开源地址：https://gitee.com/co1024/AbcMvc
// Copyright (c) 2021-2023 1024
// Abc.Mvc=Furion+EF+SqlSugar+Pear layui admin.

using Furion.EventBus;

namespace Abc.Core.EventBus;

/// <summary>
/// 事件执行器
/// </summary>
public class RetryEventHandlerExecutor : IEventHandlerExecutor
{
    public async Task ExecuteAsync(EventHandlerExecutingContext context, Func<EventHandlerExecutingContext, Task> handler)
    {
        // 如果执行失败，每隔 2s 重试，最多5次
        await Retry.InvokeAsync(async () =>
        {
            await handler(context);
        }, 5, 2 * 1000, fallbackPolicy: async (ex) =>
        {
            var _logger = AppEx.GetSeriLogger($"EventBus\\事件执行器\\{context.Source.EventId.Replace("Abc.Core;Abc.Core.EventSubscribeType.", "")}");
            _logger.Error($"事件内容：{context.Source.ToJson()}\r\n异常：{ex.ToStringEx()}");
            await Task.CompletedTask;
        });
    }
}